import { login, logout, getUserInfo, getMenus } from '@/data/apiAccount.js'
export default {
	state:{
		token:"",
		info:{
			"id": 3,
			account: "",
			realName: "",
			roles: "",
			roleNames: null,
			lastIp: "",
			lastTime: null,
			addTime: null,
			loginCount: 0,
			level: 0,
			status: false,
			phone: "",
			isSms: false,
			deptId: 0,
			postId: null,
			jobType: null,
			companyId: "",
			permissionsList: [],
		},
		menus:[]
	},
	mutations:{
		setLogin(state, data){
			state.token = data.token;
			state.info = {
				...state.info,
				...data
			}
		},
		setUserInfo(state, data){
			state.info = data
		},
		setMenu(state, data){
			state.menus = data
		}
	},
	getters:{
		isLogin: state => !!state.token
	},
	actions:{
		login({ commit }, data){
			return login(data).then(res=>{
				commit('setLogin', res)
				uni.setStorage({ key: 'token', data: res.token })
				return res;
			})
		},
		logout(){
			return logout(data).then(res=>{
				commit('setLogin', res)
				return res;
			})
		},
		getUserInfo({ commit }){
			return getUserInfo().then(res=>{
				commit('setUserInfo', res)
				return res;
			})
		},
		getMenus({ commit }){
			return getMenus().then(res=>{
				commit('setMenu', res)
				uni.clearStorage()
				return res;
			})
		}
	}
}